/*
 * Copyright 2011 Clean GWT
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.cleangwt.bootstrap.client.ui;

import com.cleangwt.bootstrap.client.style.Btn;
import com.cleangwt.bootstrap.client.style.Forms;
import com.cleangwt.uibinder.client.TemplateSupport;
import com.cleangwt.user.client.Elements;
import com.google.gwt.dom.client.DivElement;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.user.client.ui.ComplexPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * A panel for adding controls with addon styles.
 * 
 * @author Jess
 * @version 1.0
 * @since 1.0
 */
public class InputGroup extends ComplexPanel {

  private static final String INPUT_GROUP_STYLE = "input-group";

  // private static final String LG_GROUP_STYLE = "input-group-lg";
  // private static final String SM_GROUP_STYLE = "input-group-sm";

  private static final String BUTTON_STYLE = "input-group-btn";
  private static final String ADDON_STYLE = "input-group-addon";

  /**
   * Creates a button group that wraps an existing &lt;div&gt; element of template.
   * 
   * @param template the template
   * @param element the element to be wrapped
   */
  public static InputGroup wrap(TemplateSupport template, Element element) {
    InputGroup widget = new InputGroup(element);
    template.wrap(widget);
    return widget;
  }

  /**
   * Creates a input group
   */
  public InputGroup() {
    this.setElement(Document.get().createDivElement());
    this.setStyleName(INPUT_GROUP_STYLE);
  }

  /**
   * This constructor may be used by subclasses to explicitly use an existing element. This element
   * must be a &lt;div&gt; element.
   * 
   * @param element the element to be used
   */
  protected InputGroup(Element element) {
    assert element.getTagName().equalsIgnoreCase(DivElement.TAG);
    this.setElement(element);
  }

  @SuppressWarnings("deprecation")
  @Override
  public void add(Widget child) {
    add(child, getElement());
  }

  /**
   * Adds an addon span with the given html.
   * 
   * @param html the html may be a simple text
   */
  public void addAddon(String html) {
    Element span = Document.get().createSpanElement();
    span.setClassName(ADDON_STYLE);
    this.getElement().appendChild(span);
    span.setInnerHTML(html);
  }

  /**
   * Adds an addon span with the given widget.
   * 
   * @param widget the addon widget
   */
  public void addAddon(Widget widget) {
    Element span = Document.get().createSpanElement();
    span.setClassName(ADDON_STYLE);
    this.getElement().appendChild(span);
    this.add(widget, span);
  }

  /**
   * Adds a button.
   * 
   * @param btn the button to add
   */
  public void addButton(Widget btn) {
    Element span = Document.get().createSpanElement();
    span.setClassName(BUTTON_STYLE);
    this.getElement().appendChild(span);
    // ensure btn style
    if (!Elements.hasClass(Btn.BTN_STYLE, btn.getElement())) {
      Btn.style(btn);
    }
    this.add(btn, span);
  }

  /**
   * Adds a widget and sets its style to {@link Forms#FORM_CONTROL_STYLE}.
   * 
   * @param control the control widget to add
   */
  public void addControl(Widget control) {
    control.setStyleName(Forms.FORM_CONTROL_STYLE);
    this.add(control);
  }

  /**
   * Adds a button span with the given html.
   * 
   * @param html the html may just a &lt;button&gt; or &lt;a&gt; with attributes and text.
   */
  public void addButton(String html) {
    Element span = Document.get().createSpanElement();
    span.setClassName(BUTTON_STYLE);
    this.getElement().appendChild(span);
    span.setInnerHTML(html);
  }

  /**
   * Adds a button dropdown with the given toggle text and returns the {@link DropdownBuilder} to
   * build it.
   * 
   * @param toggleText the text for toggle element
   * @return the dropdown builder to build a complete button dropdown
   */
  public DropdownBuilder addDropdown(String toggleText) {
    Element dropdown = DropdownBuilder.createButtonDropdown(toggleText);
    dropdown.setClassName(BUTTON_STYLE);
    this.getElement().appendChild(dropdown);
    return new DropdownBuilder(dropdown);
  }

  // /**
  // * Resets the style name with base input group style and the given type style.
  // *
  // * @param style the base style
  // */
  // public void setStyleBase(String style){
  // assert style != null : "Stye must not be null.";
  // setStyleName(INPUT_GROUP_STYLE + " " + style);
  // }

}
